"""
修改密码 用例
"""
import os
import ddt
import unittest
import time
from common.openExcel import Open_excel
from Page.password_recovery_page import *
from Page.login_page import *
from common.databaseTool import DatabaseTool

fileName = os.path.dirname(__file__) + "/testData/修改密码测试数据.xlsx"
testdata = Open_excel(fileName, "Sheet1", conversers={"newpassword": str, "confirmpassword": str}).read()
testdata1 = Open_excel(fileName, "Sheet2", conversers={"username": str}).read()




@ddt.ddt
class PasswordRecoveryCase(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.db = DatabaseTool("168168", "ecshop_db", port=3308)

    def setUp(self):
        self.driver = open_browser()
        self.lg = PasswordRecovery_page(self.driver)
        self.lg.open_url(PR_url)

    def tearDown(self):
        """
        关闭浏览器
        :return:
        """
        self.lg.quit()

    def PR_username(self, username):
        """
        验证使用不合法（未注册）用户名进行验证找回密码
        :return:
        """
        try:

            self.lg.input_username(username)
            time.sleep(10)
            self.assertIn("ECSHOP演示站", self.lg.title(), msg="用例执行失败")
        except Exception as e:
            raise Exception("失败")

    @ddt.data(*testdata1)
    def test_PR_001(self, data):
        """
            验证使用不合法（未注册）用户名进行验证找回密码
            :return:
        """
        self.PR_username(**data)

    def test_PR_002(self):
        """
        验证密码提示问题输入错误答案
        :return:
        """
        try:
            sql = "select passwd_answer from ecs_users where user_name='admin123'"
            answer = str(self.db.fetchOne(sql)["passwd_answer"])
            self.lg.input_username("admin123")
            self.lg.input_answer("aaa")
            self.assertNotEqual("aaa", answer, msg="用例执行失败")
        except Exception as e:
            raise Exception("失败")

    def PR(self, newpassword, confirmpassword):
        """
        验证使用错误数据修改密码
        :return:
        """
        try:
            self.lg.input_username("admin123")
            self.lg.input_answer("睡觉")
            self.lg.inupt_newpassword(newpassword)
            self.lg.inupt_confirmpassword(confirmpassword)
            self.lg.cilck_submit()
            alter = self.driver.switch_to.alert  # 获取弹窗
            self.assertIn("密码不一致", alter.text, msg="用例执行失败")
            print(alter.text)
            alter.accept()  # 点击弹窗上的确定
        except Exception as e:
            raise Exception("用例执行失败")

    @ddt.data(*testdata)
    def test_PR_003(self, data):
        self.PR(**data)

    def test_PR_004(self):
        """
        验证在 输入密码提示问题页面 点击 返回上一页
        :return:
        """
        try:
            self.lg.input_username("admin123")
            self.lg.click_button()  # 点击返回上一页
            self.assertIn("请输入您注册的用户名以取得您的密码提示问题", self.lg.p_text(), msg="用例执行失败")
            print(self.lg.p_text())
        except Exception as e:
            raise Exception("用例执行失败")

    def test_PR_008(self):
        """
        验证使用正确数据修改密码
        :return:
        """

        self.lg.input_username("admin123")
        self.lg.input_answer("睡觉")
        self.lg.inupt_newpassword("789456")
        self.lg.inupt_confirmpassword("789456")
        self.lg.cilck_submit()  # 点击确定
        import time
        time.sleep(5)
        # 打开浏览器
        self.driver = open_browser()
        self.lp = Login_page(self.driver)
        # 打开网页
        self.lp.open_url(login_url)
        # 定位用户名并输入
        self.lp.input_username("admin123")
        # 定位密码并输入
        self.lp.input_password("789456")
        self.lp.click_login()
        try:
            self.user = self.lp.get_element_text(("class name", "f4_b"))
            self.assertEqual("admin123", self.user, msg="用例执行失败,当前页面登录用户名与预期不一致")
        except Exception as e:
            raise Exception("用例执行失败")


if __name__ == '__main__':
    unittest.main()
